/**
 * 
 */


(function(){
    var App = TM.App = TM.App || {};
    
    App.Event = {
        ENTER_FRAME : "EnterFrame",
        
        MOUSE_OVER  : "MouseOver",
        MOUSE_OUT   : "MouseOut",
        MOUSE_DOWN  : "MouseDown",
        MOUSE_UP    : "MouseUp",
        MOUSE_MOVE  : "MouseMove",
        
        COLLISION   : "Collision"
    };
    
})();


(function(){
    
    /**
     * インタラクティブアップデート
     */
    var InteractiveUpdate = function(game)
    {
        var prevOnMouseFlag = this.onMouseFlag;
        this.onMouseFlag    = this.isHitPoint(game.pointing.x, game.pointing.y);
        
        if (!prevOnMouseFlag && this.onMouseFlag) {
            this.dispatchEvent("onMouseOver");
        }
        
        if (prevOnMouseFlag && !this.onMouseFlag) {
            this.dispatchEvent("onMouseOut");
        }
        
        if (this.onMouseFlag) {
            if (game.pointing.getPointingStart()) {
                this.dispatchEvent("onMouseDown");
                this.mouseDowned = true;
            }
            
            this.dispatchEvent("onMouseMove");
        }
        
        if (this.mouseDowned==true && game.pointing.getPointingEnd()) {
            this.dispatchEvent("onMouseUp");
            this.mouseDowned = false;
        }
        
        // 衝突判定
        if (this.collisionList.length > 0) {
            for (var i=0,len=this.collisionList.length; i<len; ++i) {
                var target = this.collisionList[i];
                if (this.isHitElement(target)) this.dispatchEvent("onCollision", target);
            }
        }
        
        this.dispatchEvent("onEnterFrame");
        
    };

    // 
    TM.loadScript("app", "game-element", function(){
        
        if (!TM.App.GameElement) { return false; }
        
        /**
         * @lends   TM.App.InteractiveGameElement.prototype
         */
        var InteractiveGameElement = TM.App.InteractiveGameElement = TM.createClass({
            
            superClass: TM.App.GameElement,
            collisionList: null,
            
            /**
             * インタラクティブなゲームオブジェクト
             * @constructs
             * @class       インタラクティブゲームオブジェクト
             * @extends     TM.App.GameElement
             * @see <a href="./../../test/app/interactive-game-element-test.html">Test Program</a>.
             */
            init: function() {
                TM.App.GameElement.prototype.init.apply(this);
                this.collisionList = [];
            },
            
            /**
             * 更新処理
             */
            update: InteractiveUpdate,
            
            /**
             * イベントをディスパッチする
             */
            dispatchEvent: function(event_name, args) {
                this.eventFlags[event_name] = true;
                args = (args && args.length) ? args : [args];
                if (this[event_name]) this[event_name].apply(this, args);
            },
            
            isHitPoint: function(x, y) {
                if (((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y)) < this.radius*this.radius) {
                    return true;
                }
                return false;
            },
            
            /**
             * ポインティングデバイスイベントサンプル
             */
            onMouseOver : function() { console.log("mouseover");    },  // マウスオーバー
            onMouseOut  : function() { console.log("mouseout");     },  // マウスアウト
            onMouseMove : function() { console.log("mousemove");    },  // マウスムーブ
            onMouseDown : function() { console.log("mousedown");    },  // マウスダウン
            onMouseUp   : function() { console.log("mouseup");      }   // マウスアップ
            
        });
        
    });
    


    
})();











